
package
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLStream;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	import nochump.util.zip.ZipEntry;
	import nochump.util.zip.ZipFile;
	
	/**
	 * ...
	 * @author db0@qq.com
	 */
	public class FileIcon extends Sprite
	{
		private var dirArr:Array = null;
		
		public var currentType:String;
		public var url:String;
		public var fdata:ByteArray;
		public var view:DisplayObject;
		private var mainShow:MainShow = new MainShow();
		private var icon:DisplayObject = null;
		private var titleTxt:TextField = new TextField();
		
		public function FileIcon(url:String, currentType:String = null, w:int = 100, h:int = 100)
		{
			super();
			dirArr = url.split("/");
			//trace("dirArr:",dirArr,dirArr.length,dirArr.slice(0,1).length);
			
			this.currentType = currentType;
			this.url = url;
			
			titleTxt.width = w;
			titleTxt.height = h;
			titleTxt.border = true;
			addChild(titleTxt);
			
			if (stage)
				init();
			else
				addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function get filename():String
		{
			if (dirArr)
				return dirArr[dirDepth - 1];
			return null;
		}
		
		public function get dirDepth():int
		{
			if (dirArr)
				return dirArr.length;
			return 0;
		}
		
		public function get isDir():Boolean
		{
			if (title && title.charAt(titleTxt.text.length - 1) == "/")
				return true;
			return Boolean(filename == null);
		}
		
		public function get type():String
		{
			if (isDir)
				return null;
			return filename.replace(/^.*\.([^\.]+)$/i, "$1");
		}
		
		public function getDirByLevel(i:int):String
		{
			if (i <= dirDepth && i >= 0)
			{
				var s:String = dirArr.slice(0, i).join("/");
				//if (s) s = s + "/";
				return s;
			}
			return null;
		}
		
		public function get dir():String
		{
			var s:String = getDirByLevel(dirDepth - 1);
			return s;
		}
		
		public function get parentDir():String
		{
			return getDirByLevel(dirDepth - 2);
		}
		
		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			if (icon == null)
			{
				if (isDir)
				{
					[Embed(source = "system-file-manager.png")]
					var foldpng:Class;
					icon = new foldpng;
					addicon(null);
				}
				else if (filename == "..")
				{
					[Embed(source = "content-loading.png")]
					var contentpng:Class;
					icon = new contentpng;
					addicon(null);
				}
				else if (type == "zip")
				{
					[Embed(source = "package-x-generic.png")]
					var folderxpng:Class;
					icon = new folderxpng;
					addicon(null);
				}
				else
				{
					loadData(addicon);
				}
			}
			else
				addicon(null);
		
		}
		
		private function addicon(e:Event = null):void
		{
			if (e)
			{
				switch (e.type)
				{
				case Event.COMPLETE: 
					fdata = e.target.data as ByteArray;
					break;
				case "close": 
					trace("close");
					break;
				}
			}
			initView();
			if (icon == null)
				return;
			ViewSet.fullCenter(icon, 0, 0, width, height);
			icon.visible = true;
			addChildAt(icon, 0);
		}
		
		public function set title(s:String):void
		{
			titleTxt.text = s;
		}
		
		public function get title():String
		{
			return titleTxt.text;
		}
		static public var zipFile:ZipFile = null;
		
		private function initView():void
		{
			if (fdata == null)
				return;
			trace("data getted!", type);
			if (view == null)
			{
				switch (type)
				{
				case "txt": 
				case "rtf": 
				case "js": 
				case "asp": 
				case "jsp": 
				case "php": 
				case "c": 
				case "cpp": 
				case "cs": 
				case "as": 
				case "java": 
				case "htm": 
				case "html": 
				case "xml": 
					view = new MyText(0, 0, stage.stageWidth, stage.stageHeight, String(fdata), 16, 0x00000, false);
					break;
				case "jpg": 
				case "png": 
				case "jpeg": 
					view = new PhotoLoader(null, stage.stageWidth, stage.stageHeight);
					PhotoLoader(view).loadbytes(fdata);
					icon = view;
					break;
				case "zip": 
					if (zipFile == null)
					{
						zipFile = new ZipFile(fdata);
						//trace(zipFile.entries);
						view = new FileManagers(zipFile.entries, null, "zip");
					}
					break;
				case "pdf": 
					break;
				case "doc": 
					break;
				case "docx": 
					view = new DocxParser(fdata);
					break;
				case "ppt": 
					break;
				case "pptx": 
					view = new PptxParser(fdata);
					break;
				case "xls": 
					break;
				case "xlsx": 
					view = new XlsxParser(fdata);
					break;
				}
			}
		}
		
		public function dataGetted(e:Event):void
		{
			if (e && e.type == Event.COMPLETE)
			{
				fdata = e.target.data as ByteArray;
			}
			else
			{
				switch (currentType)
				{
				case "zip": 
					if (fdata == null)
					{
						trace("zipURL:", url);
						for each (var entry:ZipEntry in zipFile.entries)
						{
							if (entry.name == url)
							{
								trace("getted");
								fdata = zipFile.getInput(entry);
								break;
							}
						}
							//var entry:ZipEntry = zipFile.entries[int(name.substr(1))];
							//fdata = zipFile.getInput(entry);
					}
					break;
				}
			}
			initView();
			if (view == null)
			{
				view = ViewSet.make_txt(0, 0, "暂无法预览!");
			}
			if (view)
			{
				mainShow.addObj(view);
				mainShow.file = this;
				stage.addChild(mainShow);
				mainShow.addEventListener("close", addicon);
				mainShow.scaleX = mainShow.scaleY = 1.0;
				//ViewSet.fullCenter(view, 0, 0, stage.stageWidth, stage.stageHeight);
			}
		
		}
		
		public function loadData(loaded:Function):void
		{
			if (filename == null || filename.length == 0)
				return;
			if (isDir)
			{
				loaded(null);
				return;
			}
			if (fdata == null)
			{
				switch (currentType)
				{
				case "zip": 
					loaded(null);
					break;
				default: 
					SwfLoader.loadData(url, loaded, URLLoaderDataFormat.BINARY);
				}
			}
			else
			{
				loaded(null);
			}
		
		}
	
	}

}
